package cn.wolfcode.trip.base.domain;

import cn.wolfcode.trip.base.util.DateUtil;
import cn.wolfcode.trip.base.util.JSONUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Getter;
import lombok.Setter;

import java.util.Date;
import java.util.HashMap;

/**
 * 攻略文章
 */
@Getter
@Setter
public class StrategyDetail extends BaseDomain {
    /**
     * 草稿
     */
    public static final Integer STATE_COMMON = 0;
    /**
     * 发布
     */
    public static final Integer STATE_RELEASE = 1;
    /**
     * 禁用
     */
    public static final Integer STATE_DISABLE = -1;

    /**
     * 标题
     */
    private String title;
    /**
     * 创建时间
     */
    private Date createTime;
    /**
     * 发布时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss", timezone = "GMT+8")
    private Date releaseTime;
    /**
     * 序列号
     */
    private Integer sequence;
    /**
     * 关联目录
     */
    private StrategyCatalog catalog;
    /**
     * 封面
     */
    private String coverUrl;
    /**
     * 状态
     */
    private Integer state;
    /**
     * 攻略文本内容
     */
    private StrategyContent strategyContent;

    /**
     * 发布时间格式转换
     * @return
     */
    public String getReleaseTimeString() {
        return DateUtil.formatDate(releaseTime);
    }

    /**
     * 获取状态字符串表示
     * @return
     */
    public String getStateName() {

        if (STATE_RELEASE.equals(state)) {
            return "发布";
        } else if (STATE_DISABLE.equals(state)) {
            return "禁用";
        } else {
            return "草稿";
        }
    }

    public String getJson() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("title", title);
        if (catalog != null) {
            map.put("catalogId", catalog.getId());
            if (catalog.getStrategy() != null) {
                map.put("strategyId", catalog.getStrategy().getId());
            }
        }
        map.put("state", state);
        map.put("coverUrl", coverUrl);
        map.put("sequence", sequence);
        if (strategyContent != null) {
            map.put("strategyContent",strategyContent.getContent() );
        }
        return JSONUtil.toJSONString(map);
    }

}